package com.samoilovich.belarustoday.basefragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.datemodel.NewsModel;
import com.samoilovich.belarustoday.handlers.NewsHandler;
import com.samoilovich.belarustoday.tools.Utility;

public class NewsContentBaseFragment extends BaseFragment {
    private View mViewNoConnection;
    private ProgressBar mProgressBar;
    private ScrollView mScrollView;

    protected NewsModel mCurrentNews;
    protected String mCurrentCategoryName;
    protected WebView mWebView;
    protected long mNewsId;

    public NewsContentBaseFragment() {

    }

    public NewsContentBaseFragment(Context context, long newsId, String currentCategoryName) {
        mNewsId = newsId;
        mCurrentCategoryName = currentCategoryName;
        mInProgress = false;
        getContent(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_news_category_content, null);
        prepareViews();
        prepareActionBarViews(R.string.empty_str, mCurrentCategoryName, true);

        return mRootView;
    }

    @Override
    public void updateNewsContent(final NewsModel model) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mCurrentNews = model;
                if (mCurrentNews != null) {
                    updateScreen();
                }
                mInProgress = false;
            }
        });
    }

    @Override
    public void onReceiveInternetIntent(boolean hasInternetConnection, Intent intent) {
        if (mWebView == null || mViewNoConnection == null) {
            prepareViews();
        }
        if (!hasInternetConnection) {
            if (mCurrentNews == null || mCurrentNews.equals("")) {
                mViewNoConnection.setVisibility(View.VISIBLE);
            } else {
                mViewNoConnection.setVisibility(View.GONE);
            }
        } else {
            if (!mInProgress && (mCurrentNews == null || mCurrentNews.equals(""))) {
                getContent(getActivity());
            }
            mViewNoConnection.setVisibility(View.GONE);
        }
    }

    @Override
    public void updateAllData(Context context) {
        if (mWebView == null || mViewNoConnection == null) {
            prepareViews();
        }
        mProgressBar.setVisibility(View.VISIBLE);
        mRootView.findViewById(R.id.scrollView_newsContent).setVisibility(View.GONE);
        mWebView.clearView();
        mViewNoConnection.setVisibility(View.GONE);
        mScrollView.smoothScrollTo(0, 0);
        mScrollView.setVisibility(View.GONE);
        getContent(context);
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    protected void getContent(Context context) {
        if (Utility.hasInternetConnection(context)) {
            mInProgress = true;
            NewsHandler.getInstance().getNewsContent(this, mNewsId);
            mCurrentNews = null;
        }
    }

    private void prepareViews() {
        mViewNoConnection = mRootView.findViewById(R.id.view_noConnection);
        mWebView = (WebView) mRootView.findViewById(R.id.textView_newsContent);
        mProgressBar = (ProgressBar) mRootView.findViewById(R.id.progressBar_waitNewsContent);
        mScrollView = (ScrollView) mRootView.findViewById(R.id.scrollView_newsContent);
    }

    private void updateScreen() {
        if (mCurrentNews != null) {
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadDataWithBaseURL(null, mCurrentNews.getDescription(), "text/html", "utf-8", null);
            mProgressBar.setVisibility(View.GONE);
            mScrollView.setVisibility(View.VISIBLE);
            ((TextView) mRootView.findViewById(R.id.textView_newsContentTitle)).setText(mCurrentNews.getTitle());
            ((TextView) mRootView.findViewById(R.id.textView_newsDate)).setText(mCurrentNews.getTime());
            mWebView.setVisibility(View.VISIBLE);
        }
    }


}
